package com.xja.runner;

import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 翁
 * @date 2022/4/28 10:13
 */
@Component
public class BookServerRunner implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {

            List<FlowRule> roleList =new ArrayList<>();
            //定义单个规则
            FlowRule flowRule =new FlowRule();
            flowRule.setCount(1);//一次访问限定
            flowRule.setResource("find_rule");//定义规则名称
            flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
            flowRule.setLimitApp("default");
            roleList.add(flowRule);
            //定义单个的规则
            FlowRule flowRule1 =new FlowRule();
            flowRule1.setCount(2);
            flowRule1.setResource("find_rule");
            flowRule1.setGrade(RuleConstant.FLOW_GRADE_QPS);
            flowRule1.setLimitApp("default");
            roleList.add(flowRule1);
            FlowRuleManager.loadRules(roleList);
           // return "限流规则加载成功";
    }
}
